from pydantic import BaseModel, Field
from typing import Optional, Dict, Any, List
from datetime import datetime

class UserBehaviorBase(BaseModel):
    """用户行为基础模式"""
    action: str = Field(..., max_length=100, description="行为类型")
    target: Optional[str] = Field(None, max_length=100, description="目标对象")
    target_id: Optional[str] = Field(None, max_length=100, description="目标ID")
    metadata: Optional[Dict[str, Any]] = Field(None, description="元数据")

class UserBehaviorCreate(UserBehaviorBase):
    """创建用户行为模式"""
    ip_address: Optional[str] = Field(None, max_length=45, description="IP地址")
    user_agent: Optional[str] = Field(None, description="用户代理")
    session_id: Optional[str] = Field(None, max_length=100, description="会话ID")

class UserBehavior(UserBehaviorBase):
    """用户行为响应模式"""
    id: int
    user_id: int
    ip_address: Optional[str] = None
    user_agent: Optional[str] = None
    session_id: Optional[str] = None
    created_at: datetime
    
    class Config:
        from_attributes = True

class PageViewBase(BaseModel):
    """页面访问基础模式"""
    page_url: str = Field(..., max_length=500, description="页面URL")
    page_title: Optional[str] = Field(None, max_length=200, description="页面标题")
    referrer: Optional[str] = Field(None, max_length=500, description="来源页面")
    duration: Optional[int] = Field(None, description="停留时长(秒)")
    metadata: Optional[Dict[str, Any]] = Field(None, description="元数据")

class PageViewCreate(PageViewBase):
    """创建页面访问模式"""
    ip_address: Optional[str] = Field(None, max_length=45, description="IP地址")
    user_agent: Optional[str] = Field(None, description="用户代理")
    session_id: Optional[str] = Field(None, max_length=100, description="会话ID")

class PageView(PageViewBase):
    """页面访问响应模式"""
    id: int
    user_id: int
    ip_address: Optional[str] = None
    user_agent: Optional[str] = None
    session_id: Optional[str] = None
    created_at: datetime
    
    class Config:
        from_attributes = True

class UserSessionBase(BaseModel):
    """用户会话基础模式"""
    session_id: str = Field(..., max_length=100, description="会话ID")
    duration: Optional[int] = Field(None, description="会话时长(秒)")
    page_count: Optional[int] = Field(None, description="访问页面数")
    metadata: Optional[Dict[str, Any]] = Field(None, description="元数据")

class UserSession(UserSessionBase):
    """用户会话响应模式"""
    id: int
    user_id: int
    ip_address: Optional[str] = None
    user_agent: Optional[str] = None
    start_time: Optional[datetime] = None
    end_time: Optional[datetime] = None
    created_at: datetime
    
    class Config:
        from_attributes = True

class AnalyticsStats(BaseModel):
    """分析统计模式"""
    total_users: int = Field(..., description="总用户数")
    active_users: int = Field(..., description="活跃用户数")
    total_pageviews: int = Field(..., description="总页面访问量")
    total_behaviors: int = Field(..., description="总用户行为数")
    avg_session_duration: float = Field(..., description="平均会话时长")
    bounce_rate: float = Field(..., description="跳出率")

class UserActivityStats(BaseModel):
    """用户活动统计模式"""
    date: str = Field(..., description="日期")
    active_users: int = Field(..., description="活跃用户数")
    pageviews: int = Field(..., description="页面访问量")
    behaviors: int = Field(..., description="用户行为数")

class PopularPage(BaseModel):
    """热门页面模式"""
    page_url: str = Field(..., description="页面URL")
    page_title: Optional[str] = Field(None, description="页面标题")
    visit_count: int = Field(..., description="访问次数")
    unique_visitors: int = Field(..., description="独立访客数")
    avg_duration: float = Field(..., description="平均停留时长")

class BehaviorType(BaseModel):
    """行为类型模式"""
    action: str = Field(..., description="行为类型")
    count: int = Field(..., description="行为次数")
    unique_users: int = Field(..., description="独立用户数")

class RetentionStat(BaseModel):
    """留存统计模式"""
    period: str = Field(..., description="留存周期")
    cohort_date: str = Field(..., description="队列日期")
    total_users: int = Field(..., description="总用户数")
    retained_users: int = Field(..., description="留存用户数")
    retention_rate: float = Field(..., description="留存率")

class RealtimeStats(BaseModel):
    """实时统计模式"""
    online_users: int = Field(..., description="在线用户数")
    recent_pageviews: int = Field(..., description="最近页面访问量")
    recent_behaviors: int = Field(..., description="最近用户行为数")
    recent_pages: List[Dict[str, Any]] = Field(..., description="最近访问页面")

class DataCleanupResult(BaseModel):
    """数据清理结果模式"""
    message: str = Field(..., description="清理消息")
    cleanup_date: str = Field(..., description="清理时间点")
    deleted_behaviors: int = Field(..., description="删除的行为记录数")
    deleted_pageviews: int = Field(..., description="删除的页面访问记录数")
    deleted_sessions: int = Field(..., description="删除的会话记录数")